package com.byl.mvvm.ui.main;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.byl.mvvm.R;
import com.byl.mvvm.utils.MPLineChartManager;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineDataSet;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @ProjectName: MVVM
 * @Package: com.byl.mvvm.ui.main
 * @ClassName: lineCahtActivity
 * @Description:
 * @Author: lirui
 * @CreateDate: 2020/12/9 0009 下午 16:14
 */
public class lineCahtActivity extends AppCompatActivity {

    private MPLineChartManager mpLineChartManager;
    private View chart_btn_LineChart;
    private LineChart chart_v_LineChart;
    private View chart_btn_lc_changeTheVerValue;
    private boolean is_changeTheVerValue = true;
    private View chart_btn_lc_changeFilled;
    private boolean is_changeFilled = true;
    private View chart_btn_lc_changeTheVerCircle;
    private boolean is_changeTheVerCircle = true;
    private View chart_btn_lc_changeMode;
    private int state_changeMode = 1;
    private TextView chart_btn_lc_animationX;
    private TextView chart_btn_lc_animationY;
    private int state_animationY = 1;
    private View chart_btn_lc_animationXY;
    private View chart_btn_lc_refresh;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        initLineChart();
    }

    private void initLineChart() {
        chart_btn_LineChart = findViewById(R.id.chart_btn_LineChart);
        chart_v_LineChart = (LineChart) findViewById(R.id.chart_v_LineChart);
        chart_btn_lc_changeTheVerValue = findViewById(R.id.chart_btn_lc_changeTheVerValue);
        chart_btn_lc_changeFilled = findViewById(R.id.chart_btn_lc_changeFilled);
        chart_btn_lc_changeTheVerCircle = findViewById(R.id.chart_btn_lc_changeTheVerCircle);
        chart_btn_lc_changeMode = findViewById(R.id.chart_btn_lc_changeMode);
        chart_btn_lc_animationX = (TextView) findViewById(R.id.chart_btn_lc_animationX);
        chart_btn_lc_animationY = (TextView) findViewById(R.id.chart_btn_lc_animationY);
        chart_btn_lc_animationXY = (TextView) findViewById(R.id.chart_btn_lc_animationXY);
        chart_btn_lc_refresh = findViewById(R.id.chart_btn_lc_refresh);

        chart_btn_lc_refresh.setOnClickListener(clickListener);
        chart_btn_lc_animationXY.setOnClickListener(clickListener);
        chart_btn_LineChart.setOnClickListener(clickListener);
        chart_btn_lc_changeTheVerValue.setOnClickListener(clickListener);
        chart_btn_lc_changeFilled.setOnClickListener(clickListener);
        chart_btn_lc_changeTheVerCircle.setOnClickListener(clickListener);
        chart_btn_lc_changeMode.setOnClickListener(clickListener);
        chart_btn_lc_animationX.setOnClickListener(clickListener);
        chart_btn_lc_animationY.setOnClickListener(clickListener);
    }

    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                //  start --------------- 线形图 ------------------
                case R.id.chart_btn_lc_refresh: // 刷新
                {
                    click_lc_Refresh();
                    break;
                }
                case R.id.chart_btn_lc_animationXY:// x，y轴动画
                {
                    mpLineChartManager.animationXY(3000, 3000);
                    break;
                }
                case R.id.chart_btn_lc_animationY:// y轴动画
                {
                    click_lc_AnimationY();
                    break;
                }
                case R.id.chart_btn_lc_animationX:// x轴动画
                {
                    Random rand = new Random();
                    int duration = (rand.nextInt(20) + 1) * 1000;
                    mpLineChartManager.animationX(duration);
                    chart_btn_lc_animationX.setText("x轴动画 " + String.valueOf(duration));
                    break;
                }
                case R.id.chart_btn_lc_changeMode://切换立方
                {
                    click_lc_changeMode();
                    break;
                }
                case R.id.chart_btn_lc_changeTheVerCircle://是否显示顶点
                {
                    click_lc_changeTheVerCircle();
                    break;
                }
                case R.id.chart_btn_lc_changeFilled://是否填充
                {
                    click_lc_changeFilled();
                    break;
                }
                case R.id.chart_btn_lc_changeTheVerValue://是否显示顶点值
                {
                    click_lc_changeTheVerValue();
                    break;
                }
                case R.id.chart_btn_LineChart: //创建线形图表
                {
                    clickLineChart();
                    break;
                }
                //  end --------------- 线形图 ------------------
            }
        }
    };

    private void clickLineChart() {
        mpLineChartManager = new MPLineChartManager(chart_v_LineChart);
        mpLineChartManager.setXAxis(true, false, 10f, Color.WHITE , XAxis.XAxisPosition.BOTTOM, 0f);


        //假数据 折线1111
        ArrayList<Entry> values1 = new ArrayList<Entry>();
        values1.add(new Entry(100, 120,getResources().getDrawable(R.drawable.shape_bg_one)));
        values1.add(new Entry(120, 150,getResources().getDrawable(R.drawable.shape_bg_one)));
        values1.add(new Entry(130, 120,getResources().getDrawable(R.drawable.shape_bg_one)));
        values1.add(new Entry(140, 130,getResources().getDrawable(R.drawable.shape_bg_one)));
        values1.add(new Entry(150, 110,getResources().getDrawable(R.drawable.shape_bg_one)));
        mpLineChartManager.addData(values1, "小兵一号", 0f,  getResources().getColor(R.color.line1), 8f,
                false, 5f, Color.GREEN,
                true, getResources().getColor(R.color.line1));

        //假数据 折线2222
        ArrayList<Entry> values2 = new ArrayList<Entry>();
        values2.add(new Entry(100, 30,getResources().getDrawable(R.drawable.shape_bg_two)));
        values2.add(new Entry(120, 60,getResources().getDrawable(R.drawable.shape_bg_two)));
        values2.add(new Entry(130, 100,getResources().getDrawable(R.drawable.shape_bg_two)));
        values2.add(new Entry(140, 180,getResources().getDrawable(R.drawable.shape_bg_two)));
        values2.add(new Entry(150, 20,getResources().getDrawable(R.drawable.shape_bg_two)));
        mpLineChartManager.addData(values2, "小兵二号", 0f, getResources().getColor(R.color.line2), 8f,
                false, 5f, Color.GREEN,
                false, getResources().getColor(R.color.line2));

        mpLineChartManager.setData();
        mpLineChartManager.setInteraction(true, true, true, true, false,
                false, false, false);
        //mpLineChartManager.setLegend(Legend.LegendPosition.BELOW_CHART_LEFT, 10f, Color.BLACK, Legend.LegendForm.CIRCLE);

        mpLineChartManager.invalidate();
    }


    private void click_lc_Refresh() {
        //假数据  临时测试值，根据实情处理
        Random random = new Random();
        int n1 = random.nextInt(200);
        int n2 = random.nextInt(200);
        int n3 = random.nextInt(200);
        int n4 = random.nextInt(200);
        int n5 = random.nextInt(200);
        // 假数据 折线1111
        ArrayList<Entry> values1 = new ArrayList<Entry>();
        values1.add(new Entry(100, n1,getResources().getDrawable(R.drawable.shape_bg_one)));
        values1.add(new Entry(120, n2,getResources().getDrawable(R.drawable.shape_bg_one)));
        values1.add(new Entry(130, n3,getResources().getDrawable(R.drawable.shape_bg_one)));
        values1.add(new Entry(140, n4,getResources().getDrawable(R.drawable.shape_bg_one)));
        values1.add(new Entry(150, n5,getResources().getDrawable(R.drawable.shape_bg_one)));
        // 假数据 折线2222
        ArrayList<Entry> values2 = new ArrayList<Entry>();
        values2.add(new Entry(100, n5,getResources().getDrawable(R.drawable.shape_bg_two)));
        values2.add(new Entry(120, n4,getResources().getDrawable(R.drawable.shape_bg_two)));
        values2.add(new Entry(130, n3,getResources().getDrawable(R.drawable.shape_bg_two)));
        values2.add(new Entry(140, n2,getResources().getDrawable(R.drawable.shape_bg_two)));
        values2.add(new Entry(150, n1,getResources().getDrawable(R.drawable.shape_bg_two)));
        // 执行
        List<ArrayList<Entry>> list = new ArrayList<>();
        list.add(values1);
        list.add(values2);
        mpLineChartManager.refreshData(list);
        mpLineChartManager.invalidate();
    }


    private void click_lc_changeTheVerValue() {
        if (is_changeTheVerValue) {
            is_changeTheVerValue = false;
        } else {
            is_changeTheVerValue = true;
        }
        mpLineChartManager.changeTheVerValue(is_changeTheVerValue);
    }

    private void click_lc_changeFilled() {
        if (is_changeFilled) {
            is_changeFilled = false;
        } else {
            is_changeFilled = true;
        }
        mpLineChartManager.changeFilled(is_changeFilled);
    }

    private void click_lc_changeTheVerCircle() {
        if (is_changeTheVerCircle) {
            is_changeTheVerCircle = false;
        } else {
            is_changeTheVerCircle = true;
        }
        mpLineChartManager.changeTheVerCircle(is_changeTheVerCircle);
    }

    private void click_lc_changeMode() {
        if (state_changeMode == 1) {
            state_changeMode = 2;
            mpLineChartManager.changeMode(LineDataSet.Mode.CUBIC_BEZIER);
        } else if (state_changeMode == 2) {
            state_changeMode = 3;
            mpLineChartManager.changeMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
        } else if (state_changeMode == 3) {
            state_changeMode = 4;
            mpLineChartManager.changeMode(LineDataSet.Mode.STEPPED);
        } else if (state_changeMode == 4) {
            state_changeMode = 1;
            mpLineChartManager.changeMode(LineDataSet.Mode.LINEAR);
        }
    }

    private void click_lc_AnimationY() {
        if (state_animationY == 1) {
            state_animationY = 2;
            mpLineChartManager.animationY(4000, Easing.EasingOption.EaseInBack);
            chart_btn_lc_animationY.setText("y轴动画 EaseInBack");
        } else if (state_animationY == 2) {
            state_animationY = 3;
            mpLineChartManager.animationY(4000, Easing.EasingOption.EaseInBounce);
            chart_btn_lc_animationY.setText("y轴动画 EaseInBounce");
        } else if (state_animationY == 3) {
            state_animationY = 4;
            mpLineChartManager.animationY(4000, Easing.EasingOption.EaseInCirc);
            chart_btn_lc_animationY.setText("y轴动画 EaseInCirc");
        } else if (state_animationY == 4) {
            state_animationY = 5;
            mpLineChartManager.animationY(4000, Easing.EasingOption.EaseInCubic);
            chart_btn_lc_animationY.setText("y轴动画 EaseInCubic");
        } else if (state_animationY == 5) {
            state_animationY = 6;
            mpLineChartManager.animationY(4000, Easing.EasingOption.EaseInElastic);
            chart_btn_lc_animationY.setText("y轴动画 EaseInElastic");
        } else if (state_animationY == 6) {
            state_animationY = 7;
            mpLineChartManager.animationY(4000, Easing.EasingOption.EaseInExpo);
            chart_btn_lc_animationY.setText("y轴动画 EaseInExpo");
        } else if (state_animationY == 7) {
            state_animationY = 1;
            mpLineChartManager.animationY(4000, Easing.EasingOption.EaseInOutCubic);
            chart_btn_lc_animationY.setText("y轴动画 EaseInOutCubic");
        }
    }
}
